package com.haitaoit.chengquan.module.center.activity.info.update;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.center.activity.info.CenterInfoActivity;
import com.haitaoit.chengquan.module.center_service.activity.ServiceInfoActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UpdatePwdActivity extends BaseActivity {
    @ViewInject(R.id.et_old_password)
    EditText et_old_password;
    @ViewInject(R.id.et_new_password)
    EditText et_new_password;
    @ViewInject(R.id.et_new_password2)
    EditText et_new_password2;


    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_update_pwd);
        setTitle_V("修改密码");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);

        type = getIntent().getStringExtra("type");
    }

    @OnClick({R.id.pwd_baocun})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pwd_baocun:
                if (TextUtils.isEmpty(et_old_password.getText().toString())) {
                    ToastUtils.showToast("请输入原始密码！");
                } else if (TextUtils.isEmpty(et_new_password.getText().toString())) {
                    ToastUtils.showToast("请输入新密码！");
                } else if(et_old_password.getText().toString().equals(et_new_password.getText().toString())){
                    ToastUtils.showToast(getString(R.string.UpdatePwdActivity_newpwd_and_old_not));
                }else if (!Utils.isPassword(et_new_password.getText().toString()) ||
                        et_new_password.getText().toString().length() < 6 ||
                        et_new_password.getText().toString().length() > 12) {
                    ToastUtils.showToast("密码6-12位数字或字母！");
                } else if (!et_new_password.getText().toString().equals(et_new_password2.getText().toString())) {
                    ToastUtils.showToast("两次密码不一致！");
                }
                else {// 调用修改密码接口
                    httpGetupdate_password();
                }
                break;
        }
        super.onClick(v);
    }

    private void httpGetupdate_password() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("oldpwd", et_old_password.getText().toString()));
        list.add(new NameValuePairSign("pwd1", et_new_password.getText().toString()));
        list.add(new NameValuePairSign("pwd2", et_new_password2.getText().toString()));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("oldpwd", et_old_password.getText().toString());
        params.addQueryStringParameter("pwd1", et_new_password.getText().toString());
        params.addQueryStringParameter("pwd2", et_new_password2.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_password, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(getString(R.string.ForgetActivity_pass_modify_success));
                            PreferenceUtils.setPrefString("user_id", "");
                            PreferenceUtils.setPrefString("account_type", "");
                            PreferenceUtils.setPrefString("password", "");
                            finish();
                            if (type.equals("center")) {
                                ExitApplication.getInstance().finishActivity(CenterInfoActivity.class);
                            } else if (type.equals("service")) {
                                ExitApplication.getInstance().finishActivity(ServiceInfoActivity.class);
                            }
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
